package mtt.us.client.views.fachbereich.studiengang.editor;

import mtt.us.client.MTTUS;
import mtt.us.client.events.SetModelDataEvent;
import mtt.us.client.views.fachbereich.studiengang.events.StoreStudiengangEvent;
import mtt.us.dto.StudiengangDTO;
import mtt.us.hmvcFramework.client.hmvc.events.DisposeEvent;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import hmvc4gwt.client.event.HmvcEvent;
import hmvc4gwt.client.event.HmvcEventListener;
import hmvc4gwt.client.view.View;

public class StudiengangEditorView extends View implements HmvcEventListener {
	
	/**
	 * Button "Abbrechen"
	 */
	private Button buttonCancel = new Button();
	
	/**
	 * Button "Löschen"
	 */
	private Button buttonDelete; 
	
	/**
	 * Button "Speichern"
	 */
	private Button buttonSave = new Button();
	
	/**
	 * enthält alle Elemente
	 */
	private FlexTable table = new FlexTable();
	
	/**
	 * enthält den Namen
	 */
	private TextBox textBoxName = new TextBox();
	
	/**
	 * enthält den Vollständigen Namen des Fachbereichs
	 */
	private TextBox textBoxNameFull = new TextBox();
	
	/**
	 * enthält den zu editierenden Studiengang
	 */
	private StudiengangDTO studiengang;

	/**
	 * fügt bei vorhandenem Matrikel den Delete-Button hinzu
	 */
	private void addDeleteButton() {
		/*
		 * Button erzeugen, da er nicht immer gebraucht wird
		 */
		buttonDelete = new Button();
		buttonDelete.setText(MTTUS.constants.button_delete());
		
		/*
		 * Listener zum löschen hinzufügen
		 */
		buttonDelete.addClickListener(new ClickListener() {
			
			public void onClick(Widget sender) {
				
				if (studiengang.deleteStudiengang()) {
					getController().fireLocalEvent(new DisposeEvent());
				}
			}
		});
		
		/*
		 * Button zu Tabelle hinzufügen
		 */
		table.setWidget(4, 1, buttonDelete);
	}
	
	public void doInit() {
		
		getController().register(SetModelDataEvent.class, this);
		
		/*
		 * Button-Namen setzen 
		 */
		buttonSave.setText(MTTUS.constants.button_save());
		buttonCancel.setText(MTTUS.constants.button_cancel());
		
		/*
		 * Listener hinzufügen
		 */
		buttonSave.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				
				StoreStudiengangEvent storeStudiengangEvent = new StoreStudiengangEvent(getStudiengang());
				getController().fireEvent(storeStudiengangEvent, true, false, false);
				getController().fireLocalEvent(new DisposeEvent());
			}
		});
		
		buttonCancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				getController().fireLocalEvent(new DisposeEvent());	
			}
		});
		
		/*
		 * Name
		 */
		table.setText(1, 0, MTTUS.constants.text_name());
		table.setWidget(1, 1, textBoxName);
		
		/*
		 * vollständiger Name
		 */
		table.setText(2, 0, MTTUS.constants.text_nameFull());
		table.setWidget(2, 1, textBoxNameFull);
		
		/*
		 * Buttons hinzufügen
		 */
		table.setWidget(3, 0, buttonSave);
		table.setWidget(3, 1, buttonCancel);
	}
	
	public Widget getWidget() {
		return table;
	}

	
	public void handleEvent(HmvcEvent event) {

		if (event instanceof SetModelDataEvent) {

			if (event.getPayload() instanceof StudiengangDTO) {
				StudiengangDTO studiengang = (StudiengangDTO) event.getPayload();
				refreshView(studiengang);
			}
		}

	}
	
	private void refreshView(StudiengangDTO studiengang) {
		setStudiengang(studiengang);
		
		textBoxName.setText(studiengang.getName());
		textBoxNameFull.setText(studiengang.getNameFull());
		
		addDeleteButton();
	}

	private StudiengangDTO getStudiengang() {
		return studiengang;
	}

	private void setStudiengang(StudiengangDTO studiengang) {
		this.studiengang = studiengang;
	}
}
